package com.aiwei.tdjk.service.impl;

import com.aiwei.tdjk.bean.Pager;
import com.aiwei.tdjk.dao.extend.GroupExtendMapper;
import com.aiwei.tdjk.dao.mbg.GroupMapper;
import com.aiwei.tdjk.dto.request.GetGroupPagerRequest;
import com.aiwei.tdjk.entity.Group;
import com.aiwei.tdjk.entity.GroupExample;
import com.aiwei.tdjk.entity.UserExample;
import com.aiwei.tdjk.service.GroupRelationService;
import com.aiwei.tdjk.service.GroupService;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by yanfeng on 2016/12/13.
 */
@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private GroupExtendMapper groupExtendMapper;
    @Autowired
    private GroupRelationService groupRelationService;

    /**
     * 获取可用的的分组编号列表
     *
     * @param userId
     * @param organizationIds
     * @return
     */
    public Set<Long> findShowGroupIds(Long userId, Set<Long> organizationIds) {
        Set<Long> groupIds = Sets.newHashSet();

        groupIds.addAll(findDefaultGroupIds());
        groupIds.addAll(groupRelationService.findGroupIds(userId, organizationIds));

        //TODO 如果分组数量很多 建议此处查询时直接带着是否可用的标识去查
        for (Group group : groupMapper.selectByExample(new GroupExample())) {
            if (Boolean.FALSE.equals(group.getIsShow())) {
                groupIds.remove(group.getId());
            }
        }
        return groupIds;
    }

    public List<Long> findDefaultGroupIds(){
        GroupExample example = new GroupExample();
        example.or().andDefaultGroupEqualTo(true).andIsShowEqualTo(true);
        List<Group> groups = groupMapper.selectByExample(example);
        List<Long> list = new ArrayList<>();
        for (Group g:groups){
            list.add(g.getId());
        }
        return list;
    }

    /**
     * 获取分页列表
     *
     * @param request
     * @return
     */
    @Override
    public Pager<Group> find(GetGroupPagerRequest request) {
        request.webParamConvert();
        if (request.getType()!=null){
//            request.setType(GroupTypeEnum.);
        }
        List<Group> groupList = groupExtendMapper.find(request);
        int totalCount = groupExtendMapper.getTotalCount(request);
        Pager<Group> pager = new Pager<>();
        pager.setPageNumber(request.getPageNumber());
        pager.setPageSize(request.getPageSize());
        pager.setList(groupList);
        pager.setTotalCount(totalCount);
        return pager;
    }

    @Override
    public Group get(Long id) {
        return groupMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(Group group) {
        groupMapper.insertSelective(group);
    }

    @Override
    public int update(Group group) {
        return groupMapper.updateByPrimaryKey(group);
    }

    @Override
    public int delete(Long id) {
        return groupMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int delete(Long[] ids) {
        GroupExample example = new GroupExample();
        example.or().andIdIn(new ArrayList(Arrays.asList(ids)));
        return groupMapper.deleteByExample(example);
    }
    @Override
    public Set<Map<String, Object>> findIdAndNames(String searchType,String terms) {

        GroupExample example = new GroupExample();
        if(searchType.equals("user")){
            example.or().andNameLike("%"+terms+"%").andTypeEqualTo((byte)1);
        }else{
            example.or().andNameLike("%"+terms+"%").andTypeEqualTo((byte)2);
        }
        List<Group> groups = groupMapper.selectByExample(example);

        return Sets.newHashSet(
                Lists.transform(
                        groups,
                        new Function<Group, Map<String, Object>>() {
                            @Override
                            public Map<String, Object> apply(Group input) {
                                Map<String, Object> data = Maps.newHashMap();
                                data.put("label", input.getName());
                                data.put("domiciles","");
                                data.put("value", input.getId());
                                return data;
                            }
                        }
                )
        );


    }


}
